package com.CGFrame.struts.action.categorySet;

import java.util.Iterator;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.CGFrame.struts.POJOs.Catemodel;
import com.CGFrame.struts.POJOs.Company;
import com.CGFrame.struts.bean.ProjectOperate;
import com.CGFrame.struts.bean.ProjectOperateFactory;
import com.CGFrame.struts.bean.log.LogHistoryBean;

public class ModifyModelAction extends Action {

	@Override
	public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
		String name=request.getParameter("cateModelName");
		String modelId=request.getParameter("cateModelId");
		String type=request.getParameter("cateModelType");
		String desc=request.getParameter("modeldesc");
		ProjectOperate pop = ProjectOperateFactory.getprojectOperate();
		HttpSession session = request.getSession();
		String companyId = (String)session.getAttribute("CompanyID");
		Company c = (Company)pop.UniqueQuery("Company", "companyId,"+companyId);
		
		if(name=="" ){
			request.setAttribute("notnull","name is null!");
//			request.setAttribute( "models", c.getCatemodels());
			return mapping.findForward("showmodel");
		}
		
		List models=pop.Query("Catemodel", null,null,null);
		for(Iterator iter=models.iterator();iter.hasNext();){
			Catemodel model=(Catemodel)iter.next();
			if(model.getCateModelName()==name && model.getCateModelType()==type){
				request.setAttribute("dup", "name is duplicated!");
//				request.setAttribute( "models", c.getCatemodels());
				return mapping.findForward("showmodel");
			}		
		}
		
		Catemodel mod=(Catemodel)pop.UniqueQuery("Catemodel","cateModelId,"+modelId);
		mod.setCateModelName(name);
		mod.setCateModelType(type);
		mod.setDescription(desc);
		mod.setCompany(c);
		pop.saveObject(mod);
		
		LogHistoryBean logBean = new LogHistoryBean();
		String regUserId = (String)request.getSession().getAttribute("RegUserID");
		logBean.saveByReguserId(regUserId, LogHistoryBean.MODIFY,null, "Catemodel", Integer.parseInt(modelId) );
		
//		request.setAttribute( "models", c.getCatemodels());
		return mapping.findForward("showmodel");
	}
	
}
